package cn.itcast.mq.listener;

import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
public class SpringRabbitListener {

    @RabbitListener(queues = "simple.queue")
    public void listenSimpleQueueMessage(String msg){
        System.out.println("spring 消费者接收到的信息:【" + msg + "】");
    }

//    @RabbitListener(queues = "simple.queue")
//    public void listenWorkQueue1(String msg) throws InterruptedException {
//        System.out.println("消费者1接收到的信息:【" + msg + "】");
//        Thread.sleep(20);
//    }
//    @RabbitListener(queues = "simple.queue")
//    public void listenWorkQueue2(String msg) throws InterruptedException {
//        System.err.println("消费者2接收到的信息:【" + msg + "】");
//        Thread.sleep(200);
//    }

    @RabbitListener(queues = "fanout.queue1")
    public void listeFanoutQueue1(String msg){
        System.err.println("消费者1接收到的信息:【" + msg + "】");
    }
    @RabbitListener(queues = "fanout.queue2")
    public void listeFanoutQueue2(String msg){
        System.out.println("消费者2接收到的信息:【" + msg + "】");
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "direct.queue1"),
            exchange = @Exchange(name = "itcast.direct",
                 type = ExchangeTypes.DIRECT),
            key = {"red","blue"}
            ))
    public void listenDirectQueue1(String msg){
        System.out.println("消费者接收到direct.queue1消息:"+" "+msg);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "direct.queue2"),
            exchange = @Exchange(name = "itcast.direct",
            type = ExchangeTypes.DIRECT),
            key = {"red","yellor"}
    ))
    public void listeDirectQueue2(String msg){
        System.err.println("消费者接收到direct.queue2消息:"+" "+msg);
    }



    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "topic.queue1"),
            exchange = @Exchange(name = "itcast,topic",type = ExchangeTypes.TOPIC),
            key = "chain.#"
    ))
    public void listTopicQueue1(String msg){
        System.out.println("消费者接收到topic.queue1消息:"+" "+msg);
    }
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "topic.queue2"),
            exchange = @Exchange(name = "itcast,topic",type = ExchangeTypes.TOPIC),
            key = "*.news"
    ))
    public void listTopicQueue2(String msg){
        System.err.println("消费者接收到topic.queue2消息:"+" "+msg);
    }

}
